Ticker

6/recent/ticker-posts

Numpy Change Dimension & Numpy Selecting Element-Datacloudy

 

In this blog we are going to see about how to Change Dimension using Numpy and how to select elements in array. In addition to this we are also going to see about linspace in Numpy.

Before going to this content, if you are new to Numpy and need to go through the basic concepts in a single page read, you can check this link.

Now let us see about changing the dimension first,

Changing Dimension:

1.    Changing 1 Dimension to Multi-Dimension :

data=[1,2,3,4]

num1=data.reshape(2,2)

result:
    [[1,2],
      [3,4]]    

    In the above example  reshape(2,2) gives the 2X2 matrix. That is reshape(x,y), where x is number of rows and y is number of columns.

Note : There must be required number of elements in a single dimension  array to convert to expected multi-dimension array.

2.    Changing multidimensional to single dimensional:

 num1=[[1,2],
            [3,4]]

num1.ravel()

result:

        [1,2,3,4]

        
    In the above example we have converted the 2X2 dimension into single dimension array.

This can also be done by using,

num1.reshape(-1)    



Now let us see about selecting Element in Numpy, There are many methods based on scenarios. We will see the 3 methods in detail.

Selecting Element :

 Method1:

                 In this method, the scenario is selecting a single element,

data=[[1,2],
           [3,4]]

data[1,1]

result:
    3 
  


Therefore we can simply say this as arrayName[row index, column index]. By this way we can select elements in Numpy array.


Method 2:

                In this method, the scenario is selecting the series of elements,

data=[[1,2,3,4],
            [5,6,7,8]]

data[0:1,0:2]

result:

    1,2

Therefore we can simply say this as arrayName[row series, column series]. For selecting the element we have to think like this, in the above example the row series is [0:1] and column series is [0:2], there fore the first element of row series is 0 and the first element of column series is 0 . so, we have to select 0,0 value in the numpy array. And similarly for other element in the series. Thus in this way we can select series of elements in Numpy array.


Method 3:

                In this method, the scenario is non-series extraction and converting to our prefered dimension  using reshape. This is also called as Fancy Indexing

data=[[1,2,3,4],
            [5,6,7,8]]

data[[0,0,1,1],[0,1,2,3]].reshape(2,2)

result:

       [[1,2],
        [7,8]] 
  

    In the above example we combined the scenario of Method 1 and Method 2.

Now, we are going to see about linspace in Numpy

Linspace: 

        It is also called Linear Spacing . It returns evenly spaced numbers based on numsize.
linspace(start,end,numsize)

we can see one example,

np.linspace(0,10,5)

result:
    [0. , 2.5, 5. , 7.5, 10.]  

    In the above example, linspace(0,10,5) the first number in the  bracket is 0. It is consider to be the starting value. The second value in the bracket is 10, therefore it is consider to be the ending value. The third value in the bracket in 5, therefore it is consider to be the total number of elements. Thus linspace gives 5 values starting from 0 to 10 with equal spaced.

Hope you got a good idea about Changing Dimension in Numpy as well as selecting elements using Numpy. If you liked this blog please follow this blog by clicking follow button on the top right of this screen. Comments are always welcome.


Thank you!!!



Post a Comment

0 Comments

Ad Code